In [1]:
import sys
import os
sys.path.insert(0, os.path.abspath('../'))
sys.path.insert(0, os.path.abspath('../../'))
sys.path.insert(0, os.path.abspath('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/'))
sys.path.insert(0, os.path.abspath('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/ibm/'))
In [2]:
import numpy as np

import qiskit
provider = qiskit.IBMQ.load_account()
from qiskit import Aer
from qiskit.utils import QuantumInstance
from qiskit_optimization.algorithms import MinimumEigenOptimizer
from qiskit.algorithms import QAOA
from shared.QiskitMaxcut import *
from ibm.ibm_parameters import *

from matplotlib import pyplot as plt
%matplotlib inline

from ibm_landscape_processes import *
ibmqfactory.load_account:WARNING:2021-08-23 22:16:08,897: Credentials are already in use. The existing account in the session will be replaced.
In [3]:
%load_ext autoreload
%autoreload 2
In [4]:
# ---- Define graph and MaxCut ----
graph = load_nx_graph_from("/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/data/graphs/10_nodes/graph_10_29_01.txt")
max_cut = Maxcut(graph)
max_cut_qubo = max_cut.to_qubo()
max_cut.draw()
In [5]:
# ---- Define step size and gamma, beta values ----
step_size = 0.1
a_gamma = np.arange(-np.pi, np.pi, step_size)
b_beta = np.arange(-np.pi, np.pi, step_size)
In [6]:
gammas, betas = np.meshgrid(a_gamma, b_beta)
In [7]:
# ---- execute for all values QAOA and get result matrix  ----
landscape = run_all(gammas, betas, max_cut)
Row 0
Row 1
Row 2
Row 3
Row 4
Row 5
Row 6
Row 7
Row 8
Row 9
Row 10
Row 11
Row 12
Row 13
Row 14
Row 15
Row 16
Row 17
Row 18
Row 19
Row 20
Row 21
Row 22
Row 23
Row 24
Row 25
Row 26
Row 27
Row 28
Row 29
Row 30
Row 31
Row 32
Row 33
Row 34
Row 35
Row 36
Row 37
Row 38
Row 39
Row 40
Row 41
Row 42
Row 43
Row 44
Row 45
Row 46
Row 47
Row 48
Row 49
Row 50
Row 51
Row 52
Row 53
Row 54
Row 55
Row 56
Row 57
Row 58
Row 59
Row 60
Row 61
Row 62
In [8]:
print(landscape)
plt.matshow(landscape)
plt.show()
[[-14.50837517 -14.45562458 -14.51099968 ... -14.40687466 -14.51099968
  -14.5638752 ]
 [-14.50650024 -14.15674973 -13.85400009 ... -15.21562481 -15.00625038
  -14.73512459]
 [-14.50362492 -13.87425041 -13.29125023 ... -15.7371254  -15.45337486
  -14.99037457]
 ...
 [-14.48087502 -15.23187542 -15.81212521 ... -12.51350021 -13.12012482
  -13.87937546]
 [-14.50012493 -15.03999996 -15.47525024 ... -13.06375027 -13.48725033
  -13.96512508]
 [-14.56212521 -14.75212479 -14.94287491 ... -13.88475037 -14.01087475
  -14.27474976]]
In [9]:
# Mean of landscape
np.mean(landscape)
Out[9]:
-14.045207424408904
In [10]:
# Minimium 
np.min(landscape)
Out[10]:
-16.954500198364258
In [11]:
# Display Coordinates of Minimum 
np.unravel_index(np.argmin(landscape), landscape.shape)
Out[11]:
(35, 13)
In [12]:
# Gamma and beta value of Minimium
i, j = np.unravel_index(np.argmin(landscape), landscape.shape)
opt_gamma = gammas[i,j]
opt_beta = betas[i,j]
print(f"Opt.Gamma: {opt_gamma}, Opt.Beta: {opt_beta}")
Opt.Gamma: -1.841592653589792, Opt.Beta: 0.35840734641021
In [13]:
# Save result matrix 
with open('landscape_simulator_10_nodes_no_weights_results.npy', 'wb') as f:
    np.save(f, landscape)
In [14]:
import plotly.graph_objects as go
In [15]:
# Plot landscape in 3D 
a_gamma = np.arange(-np.pi, np.pi, step_size)
b_beta = np.arange(-np.pi, np.pi, step_size)
fig = go.Figure(data=go.Surface(z=landscape, x=a_gamma, y=b_beta))

fig.update_traces(contours_z=dict(show=True, usecolormap=True, highlightcolor='limegreen', project_z=True))


fig.update_layout(title="QAOA MaxCut", scene=dict(
    xaxis_title="gamma",
    yaxis_title="beta",
    zaxis_title="mean"
))
In [16]:
# Plot Heatmap 
fig = go.Figure(data=go.Heatmap(z=landscape, y=b_beta, x=a_gamma, type = 'heatmap', colorscale = 'viridis'))

# Update Layout
fig.update_layout(title="QAOA MaxCut", width=700, height=700, xaxis_title="beta", yaxis_title="gamma")

# Display Global Minimium 
fig.add_trace(
    go.Scatter(mode="markers", y=[opt_beta], x=[opt_gamma], marker_symbol=[204], text = [landscape[i,j]],
                   marker_color="red",  hovertemplate="x: %{x}<br>y: %{y}<br> z: %{text:.2f}<extra></extra>", 
                   marker_line_width=1, marker_size=16))
In [17]:
# Display Optimizer Results

# Display path 
#fig.add_trace(
#    go.Scatter(mode="lines", x=gammas, y=betas, marker_symbol=[200],
#                   marker_color="white", marker_line_width=1, marker_size=8)
#)

# Display start point
#fig.add_trace(
#    go.Scatter(mode="markers", x=[gammas[0]], y=[betas[0]], marker_symbol=[204],
#                   marker_color="gray", 
#                   marker_line_width=1, marker_size=16))

# Display end point
#fig.add_trace(
#    go.Scatter(mode="markers", x=[gammas[-1]], y=[betas[-1]], marker_symbol=[204],
#                   marker_color="green", 
#                   marker_line_width=1, marker_size=16))
In [18]:
# Plot Optimizer History
#fig = go.Figure(data=go.Scatter(x=counts, y=values))
#fig.update_layout(xaxis_title="Evaluation Counts", yaxis_title="Evaluated Mean", title="Optimizer")
#fig.show()